package com.erdos.message.vo;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.thoughtworks.xstream.annotations.XStreamOmitField;


public abstract class AbstractAPSMessage implements L2Messaging, XmlNameSpaceSupport {
	private static final long serialVersionUID = 7067491702091406436L;
	private static final String messageProvider=L2Messaging.class.getSimpleName();
	private static final String xmlVersionDeclaration="<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	
	private static final String baseNamespaceUri = "http://www.w3.org/2001/XMLSchema-instance";
	private static final String baseSchemaLocationUri = " ";
	/**
     * default prefix for all L2 message
     */
    private static final String PREFIX="";
    /**
     * default name space front part for L2 message
     */
    private static final String NAMESPACE="xmlns";
    /**
     * default schema location front part for L2 message
     */
    private static final String SCHEMALOCATION="schemaLocation";
    /**
     * default xsi front part for L2 message
     */
    private static final String XSI="xsi";

	private APSMessageHeader messageHeader;
	
	@XStreamOmitField
	private String messageId;
	
	public AbstractAPSMessage() {
		this.messageHeader=new APSMessageHeader();
		this.messageHeader.setMessageTypeId(this.getMessageTypeId());
	}
	
	/**
	 * @return the messageHeader
	 */
	public APSMessageHeader getMessageHeader() {
		return messageHeader;
	}

	/**
	 * @param messageHeader the messageHeader to set
	 */
	public void setMessageHeader(APSMessageHeader aPSMessageHeader) {
		this.messageHeader = aPSMessageHeader;
	}

	
		public String getCorrelationMessageId() {
			return this.messageHeader.getCorrelationMessageId();
		}

		/* (non-Javadoc)
		 * @see com.hp.message.vo.L2Messaging#getCorrelationMessageTypeId()
		 */
		public String getCorrelationMessageTypeId() {
			return this.messageHeader.getCorrelationMessageTypeId();
		}

		/* (non-Javadoc)
		 * @see com.hp.message.vo.L2Messaging#getlength()
		 */
		public Integer getLength() {
			return this.messageHeader.getLength();
		}

		/* (non-Javadoc)
		 * @see com.hp.message.vo.L2Messaging#getMessageId()
		 */
		public String getMessageId() {
			return this.messageId;
		}

		/* (non-Javadoc)
		 * @see com.hp.message.vo.L2Messaging#getReceiver()
		 */
		public String getReceiver() {		
			return this.messageHeader.getReceiver();
		}

		/* (non-Javadoc)
		 * @see com.hp.message.vo.L2Messaging#getSendDateTime()
		 */
		public Date getSendDateTime() {		
			return this.messageHeader.getSendDateTime();
		}

		/* (non-Javadoc)
		 * @see com.hp.message.vo.L2Messaging#getSender()
		 */
		public String getSender() {
			return this.messageHeader.getSender();
		}	
		
		public void setMessageId(String msgId) {
			this.messageId = msgId;
		}
		
		public String getMessageProvider() {
			return messageProvider;
		}

		/**
	     * default prefix string.
	     */
	    public String getPrefix() {
	        return PREFIX;
	    }

		public String getXmlVersionDeclaration() {
	        return xmlVersionDeclaration;
	    }

		/**
	     * default name space string.
	     */
	    public final String getNameSpace() {
	        String nsUri=this.getNameSpaceUri();
	        String slUri=this.getSchemaLocationUri();
	        StringBuilder ns=new StringBuilder("");
	        if(StringUtils.isNotEmpty(nsUri)) {         
	            ns.append(NAMESPACE).append(":").append(XSI).append("=\"").append(nsUri).append("\"")
	            .append(" ").append(XSI).append(":").append(SCHEMALOCATION).append("=\"").append(slUri).append("\"");             
	        }

	        return ns.toString();
	    }

	    /**
	     * for subclasses to extend to supply the name space URI.
	     * @return URI of the name space
	     * @author zxu
	     * @since  2008-12-2
	     */
	    protected String getNameSpaceUri() {
	        return baseNamespaceUri;
	    }
	    
	    /**
	     * for subclasses to extend to supply the schema location URI.
	     * @return URI of the schema location
	     * @author zxu
	     * @since  2008-12-2
	     */
	    protected String getSchemaLocationUri() {
	        return baseSchemaLocationUri;
	    }

		public String toString() {
			return new ToStringBuilder(this).append(this.messageHeader).toString();
		}
}
